import Vue from 'vue';
import Vuex from 'vuex';
import Cookies from 'js-cookie';
import Base64 from 'assets/js/base64.js';
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		site_info: {},
		userInfo: {},

		default_theme_config: {
			titleBarBg: '#161921',
			titleBarColor: '#d0d5e9',
			titleBarHover: '#729aea',
			titleBarStress: '#E8EAF4',

			navBarBg: '#545C64',
			navBarColor: '#d0d5e9',
			navBarSelect: '#ffd04b',
			navBarIcon: '#909399'
		},
		theme_config: {},
		site_icon_url: ''
	},
	actions: {
		loadSiteInfo({commit}) {
			return Vue.prototype.$axios.get('/company/getInfo').then(res => {
        if(res.s == 1) {
          this.commit('updateSiteInfo', res.d);
        }
      });
		},
		loadTheme({commit}) {
			return Vue.prototype.$axios.get('/theme/getTheme').then(res => {
        if(res.s == 1) {
          this.commit('updateTheme', res.d);
        }
      });
		},
		loadSiteConfig({commit}) {
			return Vue.prototype.$axios.get('/theme/getSiteConfig').then(res => {
        if(res.s == 1) {
        	let site_config = res.d;
          this.commit('updateTheme', site_config.theme);
          this.commit('updateSiteInfo', site_config.site_info);
        }
			});
		},
		loadAdminInfo({commit}) {
			return Vue.prototype.$axios.get('/admin/adminInfo').then(res => {
        if(res.s == 1) {
          this.commit('updateUserInfo', res.d);
        }
			});
		}
	},
	getters: {
		site_info: state => {
			let site_info = state.site_info;
			if(!site_info.siteTitle) {
				site_info.siteTitle = '默认网站标题'
			}
			return site_info;
		},
		theme_config: state => {
			let theme_config = state.theme_config;
			let default_theme_config = state.default_theme_config;
			return {
				titleBarBg: (theme_config.titleBarBg ? theme_config : default_theme_config)['titleBarBg'],
				titleBarColor: (theme_config.titleBarColor ? theme_config : default_theme_config)['titleBarColor'],
				titleBarHover: (theme_config.titleBarHover ? theme_config : default_theme_config)['titleBarHover'],
				titleBarStress: (theme_config.titleBarStress ? theme_config : default_theme_config)['titleBarStress'],
				navBarBg: (theme_config.navBarBg ? theme_config : default_theme_config)['navBarBg'],
				navBarColor: (theme_config.navBarColor ? theme_config : default_theme_config)['navBarColor'],
				navBarSelect: (theme_config.navBarSelect ? theme_config : default_theme_config)['navBarSelect'],
				navBarIcon: (theme_config.navBarIcon ? theme_config : default_theme_config)['navBarIcon']
			}
		}
	},
	mutations: {
		updateSiteInfo: (state, new_val) => {
			state.site_info = new_val || {};
		},
		updateTheme: (state, new_val) => {
			state.theme_config = new_val;
		},
		updateUserInfo: (state, new_val) => {
	  	state.userInfo = new_val || {};
		},
		updateSiteIcon: (state, new_val) => {
			state.site_icon_url = new_val ? new_val : Vue.prototype.$remote_server + '/static/favicon.png?t=' + Date.now();
		}
	}
})
export default store;